﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.IO;
using Newtonsoft.Json;

namespace BasePlatformCommon
{
	[JsonObject(MemberSerialization.OptIn)]
	public class MainControl : Device
	{
		[JsonProperty]
		public PInfoData ProductionInfo { get; set; }		// 产品信息

		[JsonProperty]
		public int MainControlType { get; set; }			// 主控类型: 0-网口主控, 1-dvi主控

		[JsonProperty]
		public int DeclareMainPortNum { get; set; }			// 声明的主控端口数, 网口主控：1端口, DVI主控：2端口

		[JsonProperty]
		public int TotalMainPortNum { get; set;	}			// 总主控端口数

		[JsonProperty]
		public int TotalSubControlNum { get; set; }			// 总分控数

		[JsonProperty]
		public int ErrorSubControlNum { get; set; }			// 错误分控数

		[JsonProperty]
		public int TotalLampNum { get; set; }				// 总灯点数

		[JsonProperty]
		public int ErrorLampNum { get; set; }				// 错误灯点数

		[JsonProperty]
		public List<MainPort> MainPortList { get; set; }	// 主控端口数据集合

		// 构造函数, 0-网口主控, 1-dvi主控
		public MainControl(int type)
		{
			deviceType = DeviceType.MainControl;

			if (type == 0)
			{
				this.name = "网口";
			}
			else
			{
				this.name = "主控";
			}

			if (LanguageOper.available == true)
			{
				if (type == 0)
				{
					this.name = LanguageOper.ReadValue("Chain", "main.netName");
				}
				else
				{
					this.name = LanguageOper.ReadValue("Chain", "main.comName");
				}
			}

			ProductionInfo = new PInfoData();
			MainPortList = new List<MainPort>();
			ErrorSubControlNum = 0;
			ErrorLampNum = 0;
			TotalLampNum = 0;
			TotalSubControlNum = 0;
			MainControlType = type;

			if (MainControlType == 0)
			{
				DeclareMainPortNum = 1;		// 网口主控：1端口
			}
			else
			{
				DeclareMainPortNum = 2;		// DVI主控：2端口
			}
		}

		// 无参构造函数, 默认为网口主控, 1端口
		public MainControl()
		{
			this.name = "主控";
			if (LanguageOper.available == true)
			{
				this.name = LanguageOper.ReadValue("Chain", "main.comName");
			}

			ProductionInfo = new PInfoData();
			MainPortList = new List<MainPort>();
			ErrorSubControlNum = 0;
			ErrorLampNum = 0;
			TotalLampNum = 0;
			TotalSubControlNum = 0;
			MainControlType = 0;
			DeclareMainPortNum = 1;
			TotalMainPortNum = 1;
		}

		#region Json交互相关操作

		// 设置主控数据
		public void setMainControlData(JObject jsonData)
		{
			parseMainPortListData(jsonData);
			
			parseMainControlState();
		}

		// 解析主控端口集合数据
		private void parseMainPortListData(JObject jsonData)
		{
			try
			{
				// 通用属性
				ProductionInfo.Manufacturer = (string)jsonData["Manufacturer"];
				ProductionInfo.ProductionDate = (string)jsonData["ProductionDate"];
				ProductionInfo.ProductionType = (string)jsonData["ProductionType"];
				ProductionInfo.SerialNumber = (string)jsonData["SerialNumber"];
				ProductionInfo.Version = (string)jsonData["Version"];

				// 独立属性
				MainControlType = (int)jsonData["MainControlType"];
				DeclareMainPortNum = (int)jsonData["DeclareMainPortNum"];
				TotalMainPortNum = (int)jsonData["TotalMainPortNum"];

				// 解析每个端口数据
				for (int portIndex = 0; portIndex < TotalMainPortNum; portIndex++)
				{
					// 获取当前端口 json 数据
					string portNameStr = "MainPort" + portIndex.ToString();
					parseMainPortData((JObject)jsonData[portNameStr]);
				}
			}
			catch
			{
				Log.DebugLogger.Error("MainPortListData Parse Error!");
			}
		}

		// 解析主控端口数据
		private void parseMainPortData(JObject jsonData)
		{
			MainPort mainPort = new MainPort();
			mainPort.setMainPortData(jsonData);
			MainPortList.Add(mainPort);
		}

		// 解析主控状态 - 包含的错误灯点以及错误分控数
		private void parseMainControlState()
		{
			TotalMainPortNum = MainPortList.Count;
			for (int index = 0; index < TotalMainPortNum; index++)
			{
				TotalSubControlNum += MainPortList[index].TotalSubControlNum;
				TotalLampNum += MainPortList[index].TotalLampNum;
				ErrorLampNum += MainPortList[index].ErrorLampNum;
				ErrorSubControlNum += MainPortList[index].ErrorSubControlNum;
			}
		}

		#endregion

		// 汇总主控统计信息
		public void calcMainPortTotalInfo()
		{
			// 总主控端口数
			TotalMainPortNum = MainPortList.Count;

			// 总分控数、异常分控数、总灯具数、异常灯具数
			TotalSubControlNum = 0;
			ErrorSubControlNum = 0;
			TotalLampNum = 0;
			ErrorLampNum = 0;
			foreach (MainPort mainPort in MainPortList)
			{
				TotalSubControlNum += mainPort.TotalSubControlNum;
				ErrorSubControlNum += mainPort.ErrorSubControlNum;
				TotalLampNum += mainPort.TotalLampNum;
				ErrorLampNum += mainPort.ErrorLampNum;
			}
		}

		// 拼接提示信息
		public string buildTipMessage()
		{
			return "";
		}
	}
}
